/**
 * 通话记录
 * @authors supmain (mtingfeng@gmail.com)
 * @date    2015-02-03 14:28:55
 * @for call_log.html
 */
var BSTYUN = BSTYUN || {};
BSTYUN.callog = {
	init: function(){
		var that = this;
		/*搜索查询*/
		that.softRecord();
		/*加载数据*/
		that.goSoftResult({ startime: '' , endtime: '' , page : 1});
	},
	softRecord: function(){
		var $telrecord = $("#telrecord"),
			$startime = $("#startime"),
			$endtime = $("#endtime"),
			$recordtable = $("#recordtable");
		var $pagecontroller = $(".pagecontroller");
		/* 时间过滤 */
		$telrecord.on("keydown","#startime,#endtime",function(){
			return false;
		}).on("click",".confirm",function(){
			var startime = $.trim( $startime.val() ),
				endtime = $.trim( $endtime.val() );
			var stime = (new Date( startime.replace(/-/gi,"/") )).getTime() ,
				etime = (new Date( endtime.replace(/-/gi,"/") )).getTime() ;
			if( stime > etime ){
				BSTYUN.dialog.warn("起始时间不能大于结束时间！");
				return false;
			};
			BSTYUN.callog.goSoftResult( { startime: startime , endtime: endtime , page : 1} );
		});
		/* 分页 */
		$pagecontroller.on("click","a",function(){
			var $this = $(this),
				startime = $.trim( $("#startime").val() ),
				endtime = $.trim( $("#endtime").val() );
			if( $this.hasClass( "go2index" ) ){
				BSTYUN.callog.goSoftResult( { startime: startime , endtime: endtime , page : 1} );
			}else if( $this.hasClass( "go2prev" ) ){
				var currPage = parseInt( $.trim( $('.nowPage').html() ));
				var lastPage = parseInt($.trim( $('.totalPage').html() ));
				if(currPage <= 1) {
					BSTYUN.dialog.warn("已经是当前第一页");
				}else {
					currPage -= 1;
					BSTYUN.callog.goSoftResult( { startime: startime , endtime: endtime , page : currPage} );
				}
			}else if( $this.hasClass( "go2next" ) ){
				var currPage = parseInt( $.trim( $('.nowPage').html() ));
				var lastPage = parseInt($.trim( $('.totalPage').html() ));
				if(currPage >= lastPage) {
					BSTYUN.dialog.warn("已经是当前最后一页");
				}else {
					currPage += 1;
					BSTYUN.callog.goSoftResult( { startime: startime , endtime: endtime , page : currPage} );
				}
			}else if( $this.hasClass( "go2end" ) ){
				var lastPage = $.trim( $('.totalPage').html() );
				BSTYUN.callog.goSoftResult( { startime: startime , endtime: endtime , page : lastPage} );
			}
		}).on("change","#currPage",function(){
			var $this = $(this) ,
				index = $this.val(),
				startime = $.trim( $("#startime").val() ),
				endtime = $.trim( $("#endtime").val() );
			BSTYUN.callog.goSoftResult( { startime: startime , endtime: endtime , page : index} );
		});
	},
	/*查询用户账单*/
	goSoftResult : function( data ){
		$.ajax({
			url: '/finance/searchBill',
			type: 'post',
			dataType: 'json',
			data: data,
			success: function( data ){
				var html = '';
				if(data.callBills.length > 0) {
					$.each(data.callBills,function(index,callBill){
						html += '<tr><td>'+callBill.id+'</td>' + 
							    '<td class="calltime">'+BSTYUN.callog.timeTrim(callBill.starttime)+'</td>' +
							    '<td>IP电话</td>';
						if(callBill.caller == data.virtualPhone) {
							html += '<td>主叫</td><td>'+callBill.called+'</td>';
						}else {
							html += '<td>被叫</td><td>'+callBill.caller+'</td>';
						}
						
						html += '<td>'+BSTYUN.callog.timeSpace(callBill.starttime,callBill.stoptime)+'</td><td>0.00</td><td>0.00</td><td>0.00</td></tr>';
					})
				}
				// 遍历数据
				$('#callbill').html( html );
				
				/*分页数据设置*/
				$('.totalRows').html(data.count);
				var totalPage = data.totalPage;
				$('.nowPage').html(data.page);
				$('.totalPage').html(totalPage);
				var option = '';
				if(totalPage > 1) {
					for(var i=1; i<=totalPage; i++) {
						if(i == data.page) {
							option += '<option value="'+i+'" selected="selected">'+i+'</option>';
						}else {
							option += '<option value="'+i+'">'+i+'</option>';
						}
					}
				}else {
					option += '<option value="1" selected="selected">1</option>';
				}
				$('#currPage').html(option);
			},
			error: function(){
				//BSTYUN.dialog.warn("网络异常！");
			}
		});
	},
	/*计算时间差*/
	timeSpace : function(startTime,endTime) {
		var firstTime = new Date(startTime.replace(/-/g, "/")).getTime(); //begintime 为开始时间

		var secondTime = new Date(endTime.replace(/-/g, "/")).getTime();   // endtime 为结束时间
		
		var timeSpace = secondTime - firstTime;
		
		//计算出小时数
		var leave1= timeSpace%(24*3600*1000) ;   //计算天数后剩余的毫秒数
		var hours=Math.floor(leave1/(3600*1000));
		//计算相差分钟数
		var leave2=leave1%(3600*1000);        //计算小时数后剩余的毫秒数
		var minutes=Math.floor(leave2/(60*1000));
		//计算相差秒数
		var leave3=leave2%(60*1000);     //计算分钟数后剩余的毫秒数
		var seconds=Math.round(leave3/1000);
		
		var timeStr = '';
		if(hours > 0) {
			timeStr += hours+'小时';
		}
		if(minutes > 0) {
			timeStr += minutes+'分';
		}
		if(seconds > 0) {
			timeStr += seconds+'秒';
		}
		
		if(hours == 0 && minutes == 0 && seconds == 0) {
			timeStr = '0秒';
		}
		return timeStr;
	},
	/*去除时间后面的.0*/
	timeTrim : function(timeStr) {
		var timeStr = timeStr.replace(".0","");
		return timeStr;
	}
};
$(function(){
	BSTYUN.callog.init();
});